const express = require("express");
const pool = require("../pool.js");
let router = express.Router();
router.post('/register', (req, res, next) => {
  //获取post传递的数据
  var obj = req.body;
  if (obj.uname === '') {
    res.send({
      code: 401,
      msg: 'uname不能为空'
    });
    return;
  } else if (!obj.upwd) {
    res.send({
      code: 402,
      msg: 'upwd不能为空'
    });
    return;
  } else if (!obj.uphone) {
    res.send({
      code: 404,
      msg: 'phone不能为空'
    });
    return;
  } else if (!/^1[3-9]\d{9}$/.test(obj.uphone)) {
    res.send({
      code: 405,
      msg: 'phone格式错误'
    });
    return;
  }
  //执行SQL命令
  pool.query('insert into xhy_user set?', [obj], (err, result) => {
    if (err) {
      //把错误抛给下一个中间件处理
      next(err);
      //阻止往后执行
      return;
    };
    res.send({
      code: 200,
      msg: '注册成功'
    });
  });
});
module.exports = router;